package com.example.managementjava.Leave;

import com.example.managementjava.DB_message;
import com.example.managementjava.Student.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DB_Leave {
    private static String url = DB_message.getUrl();
    private static String user= DB_message.getUser();
    private static String pswd = DB_message.getPswd();

    public static List<LeaveNote> selectAlllist(){
        List<LeaveNote> list= new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, user, pswd);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select  * from leaves");
            while (resultSet.next()){
                String name = resultSet.getString(1);
                String sid = resultSet.getString(2);
                String gender = resultSet.getString(3);
                String phone = resultSet.getString(4);
                String special = resultSet.getString(5);
                String home = resultSet.getString(6);
                String date = resultSet.getString(7);
                String reason = resultSet.getString(8);
                String endtime = resultSet.getString(9);
                String Static = resultSet.getString(10);
                String affirm = resultSet.getString(11);
                LeaveNote leaveNote = new LeaveNote(name,sid,gender,phone,special,
                        home,date,reason,endtime,Static,affirm);
//                System.out.println(student.toString());
                list.add(leaveNote);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    public static void editLeave(String msg){
        List<Student> list= new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user="root";
            String pswd="123456";
            connection = DriverManager.getConnection(url, user, pswd);
            statement = connection.createStatement();
            String sql = "update leaves  set " +msg;

            System.out.println(sql);
            statement.executeUpdate(sql);
            System.out.println("修改成功");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    // 添加学生
    public static void addLeave(String msg){
        List<Student> list= new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user="root";
            String pswd="123456";
            connection = DriverManager.getConnection(url, user, pswd);
            statement = connection.createStatement();
            String sql="insert into leaves(name,sid,gender,phone,special," +
                    "home,date,reason,endtime) values " +msg;

            System.out.println(sql);
            statement.executeUpdate(sql);
            System.out.println("添加成功");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

}
